<template>
  <div class="page-container">
    <div class="common-content-wrap">
      <TrTable
        :btnList="btnList"
        :total="totalNum"
        isNeedIndex
        v-model="searchParams"
        :searchShowList="searchShowList"
        :tableColumn="tableColumn"
        requestName="searchHandle"
        :tableData="assTableData"
        @export="operateHandle($event, 'export')"
      />
    </div>
  </div>
</template>

<script>
import moment from "moment";
import { comExport } from "@/utils/util";
import TrTable from "@/components/Tr-table/Tr-table.vue";
import { getList, exportTableData } from "@/api/baseCode/materialCategory";
export default {
  components: {
    TrTable,
  },
  name: "SupplierBox",
  data: () => ({
    tableColumn: [
      {
        type: "text",
        label: "类别编码",
        value: "categoryCode",
      },
      {
        type: "text",
        label: "类别名称",
        value: "categoryName",
      },
    ],
    searchShowList: [
      {
        id: "categoryCode",
        type: "input",
        width: 33.33,
        label: "类别编码",
        value: "",
      },
      {
        id: "categoryName",
        type: "input",
        width: 33.33,
        label: "类别名称",
        value: "",
      },
    ],

    btnList: [
      // {
      //   label: "导出",
      //   option: "export",
      //   color: "blue",
      // },
    ],
    assTableData: [],
    searchParams: {
      current: 1,
      size: 10,
    },
    totalNum: 0,
    isShowEdit: false,
    exportParams: {},
  }),
  methods: {
    operateHandle(val, type) {
      switch (type) {
        case "export":
          this.exportHandle();
          // console.log("导出！！");
          break;
        default:
          console.log("11");
          break;
      }
    },
    exportHandle() {
      this.$textShow({
        title: "导出",
        content: [this.$t("tip.export")],
      })
        .then(() => {
          this.$load.show();
          this.exportParams.size = 1000;
          exportTableData(this.exportParams || this.searchParams)
            .then((res) => {
              if (res.status != 200)
                return this.$message.error(this.$t("msg.exportFailed"));
              let timeStr = moment().format("YYYYMMDD-HHmmss");
              const fileName = `物料分类-${timeStr}` + ".xlsx";
              comExport(res.data, fileName);
              this.$message.success(this.$t("msg.exportSuccess"));
            })
            .finally(() => {
              this.$load.close();
            });
        })
        .catch(() => {});
    },
    searchHandle() {
      this.exportParams = JSON.parse(JSON.stringify(this.searchParams));
      this.getMenuList();
    },
    getMenuList() {
      getList(this.searchParams).then((res) => {
        if (res.data.code != 200) return;
        this.assTableData = res.data.data.records;
        this.totalNum = res.data.data.total;
      });
    },
  },
  mounted() {
    // this.getMenuList();
  },
};
</script>

<style></style>
